{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>名称</th>\n",
       "      <th>类别</th>\n",
       "      <th>单价</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-07-01</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>20</td>\n",
       "      <td>2</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-07-02</td>\n",
       "      <td>商品B</td>\n",
       "      <td>服装</td>\n",
       "      <td>200</td>\n",
       "      <td>3</td>\n",
       "      <td>600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-07-03</td>\n",
       "      <td>商品C</td>\n",
       "      <td>食品</td>\n",
       "      <td>1200</td>\n",
       "      <td>4</td>\n",
       "      <td>4800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-07-04</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>22</td>\n",
       "      <td>5</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-07-05</td>\n",
       "      <td>商品B</td>\n",
       "      <td>服装</td>\n",
       "      <td>220</td>\n",
       "      <td>6</td>\n",
       "      <td>1320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2018-07-06</td>\n",
       "      <td>商品C</td>\n",
       "      <td>食品</td>\n",
       "      <td>1000</td>\n",
       "      <td>7</td>\n",
       "      <td>7000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2018-07-07</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2018-07-08</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>800</td>\n",
       "      <td>1</td>\n",
       "      <td>800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2018-07-09</td>\n",
       "      <td>商品C</td>\n",
       "      <td>食品</td>\n",
       "      <td>1300</td>\n",
       "      <td>4</td>\n",
       "      <td>5200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2018-07-10</td>\n",
       "      <td>商品B</td>\n",
       "      <td>服装</td>\n",
       "      <td>230</td>\n",
       "      <td>3</td>\n",
       "      <td>690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2018-07-11</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>28</td>\n",
       "      <td>1</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期   名称  类别    单价  数量    金额\n",
       "0  2018-07-01  商品A  服装    20   2    40\n",
       "1  2018-07-02  商品B  服装   200   3   600\n",
       "2  2018-07-03  商品C  食品  1200   4  4800\n",
       "3  2018-07-04  商品A  服装    22   5   110\n",
       "4  2018-07-05  商品B  服装   220   6  1320\n",
       "5  2018-07-06  商品C  食品  1000   7  7000\n",
       "6  2018-07-07  商品A  服装    30   3    90\n",
       "7  2018-07-08  商品A  服装   800   1   800\n",
       "8  2018-07-09  商品C  食品  1300   4  5200\n",
       "9  2018-07-10  商品B  服装   230   3   690\n",
       "10 2018-07-11  商品A  服装    28   1    28"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=pd.read_excel('data.xlsx')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pandas.core.groupby.generic.DataFrameGroupBy object at 0x0000024340479808>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped = df.groupby('类别')\n",
    "grouped"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "服装\n",
      "           日期   名称  类别   单价  数量    金额\n",
      "0  2018-07-01  商品A  服装   20   2    40\n",
      "1  2018-07-02  商品B  服装  200   3   600\n",
      "3  2018-07-04  商品A  服装   22   5   110\n",
      "4  2018-07-05  商品B  服装  220   6  1320\n",
      "6  2018-07-07  商品A  服装   30   3    90\n",
      "7  2018-07-08  商品A  服装  800   1   800\n",
      "9  2018-07-10  商品B  服装  230   3   690\n",
      "10 2018-07-11  商品A  服装   28   1    28\n",
      "食品\n",
      "          日期   名称  类别    单价  数量    金额\n",
      "2 2018-07-03  商品C  食品  1200   4  4800\n",
      "5 2018-07-06  商品C  食品  1000   7  7000\n",
      "8 2018-07-09  商品C  食品  1300   4  5200\n"
     ]
    }
   ],
   "source": [
    "for name,data in grouped:\n",
    "    print(name)\n",
    "    print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\users\\justin.zou\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\ipykernel_launcher.py:1: FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead.\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>类别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>服装</th>\n",
       "      <td>24</td>\n",
       "      <td>3678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>食品</th>\n",
       "      <td>15</td>\n",
       "      <td>17000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    数量     金额\n",
       "类别           \n",
       "服装  24   3678\n",
       "食品  15  17000"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped['数量','金额'].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "类别\n",
       "服装     800\n",
       "食品    1300\n",
       "Name: 单价, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped['单价'].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "类别  名称 \n",
       "服装  商品A     800\n",
       "    商品B     230\n",
       "食品  商品C    1300\n",
       "Name: 单价, dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped = df.groupby(['类别','名称'])\n",
    "grouped['单价'].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "类别  名称 \n",
       "服装  商品A     180.000000\n",
       "    商品B     216.666667\n",
       "食品  商品C    1166.666667\n",
       "Name: 单价, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped['单价'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>单价</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>类别</th>\n",
       "      <th>名称</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">服装</th>\n",
       "      <th>商品A</th>\n",
       "      <td>180.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>商品B</th>\n",
       "      <td>216.666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>食品</th>\n",
       "      <th>商品C</th>\n",
       "      <td>1166.666667</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 单价\n",
       "类别 名称              \n",
       "服装 商品A   180.000000\n",
       "   商品B   216.666667\n",
       "食品 商品C  1166.666667"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped[['单价']].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "服装\n",
      "['商品A' '商品B']\n",
      "食品\n",
      "['商品C']\n"
     ]
    }
   ],
   "source": [
    "grouped=df.groupby('类别')\n",
    "for name,data in grouped:\n",
    "    print(name)\n",
    "    print(data['名称'].unique())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# time datatime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1584335732.7165945"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.time() #1970年1月1号到现在的秒数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2020, tm_mon=3, tm_mday=15, tm_hour=21, tm_min=25, tm_sec=57, tm_wday=6, tm_yday=75, tm_isdst=0)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime(1584278757.0630786)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2020-03-15 21:25:57'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(1584278757.0630786))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "local=time.strptime('1987-08-22 16:53','%Y-%m-%d %H:%M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=1987, tm_mon=8, tm_mday=22, tm_hour=16, tm_min=53, tm_sec=0, tm_wday=5, tm_yday=234, tm_isdst=-1)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "local"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "556620780.0"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.mktime(local)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2020, 3, 16, 13, 15, 32, 886592)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now=datetime.now()\n",
    "now"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2020"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2020-03-16 13:15:32'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.strftime('%Y-%m-%d %H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "local=time.strptime(\"1987-08-22 16:53\",'%Y-%m-%d %H:%M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "local=datetime.strptime(\"1987-08-22 16:53:30\",'%Y-%m-%d %H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(1987, 8, 22, 16, 53, 30)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "local"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1584335732.886592"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.timestamp()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.timedelta(days=665, seconds=47733, microseconds=75595)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta=datetime.now()-datetime(2018,5,21)\n",
    "delta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "665"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delta.days"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pandas时间序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-05-01', '2018-05-02', '2018-05-03', '2018-05-04',\n",
       "               '2018-05-05', '2018-05-06', '2018-05-07', '2018-05-08',\n",
       "               '2018-05-09', '2018-05-10',\n",
       "               ...\n",
       "               '2018-09-22', '2018-09-23', '2018-09-24', '2018-09-25',\n",
       "               '2018-09-26', '2018-09-27', '2018-09-28', '2018-09-29',\n",
       "               '2018-09-30', '2018-10-01'],\n",
       "              dtype='datetime64[ns]', length=154, freq='D')"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#初始化时间序列\n",
    "pd.date_range('2018-5-1','2018-10-1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-05-06', '2018-05-13', '2018-05-20', '2018-05-27',\n",
       "               '2018-06-03', '2018-06-10', '2018-06-17', '2018-06-24',\n",
       "               '2018-07-01', '2018-07-08', '2018-07-15', '2018-07-22',\n",
       "               '2018-07-29', '2018-08-05', '2018-08-12', '2018-08-19',\n",
       "               '2018-08-26', '2018-09-02', '2018-09-09', '2018-09-16',\n",
       "               '2018-09-23', '2018-09-30'],\n",
       "              dtype='datetime64[ns]', freq='W-SUN')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2018-5-1','2018-10-1',freq='w')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "#D 天，W周，M月，Q季度，H小时，T分，S秒"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-06-30', '2018-09-30', '2018-12-31', '2019-03-31',\n",
       "               '2019-06-30', '2019-09-30', '2019-12-31', '2020-03-31',\n",
       "               '2020-06-30', '2020-09-30'],\n",
       "              dtype='datetime64[ns]', freq='Q-DEC')"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2018-05-01',freq='Q',periods=10) #从5月1日开始返回10个季度周期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-08-19 00:00:00</td>\n",
       "      <td>9.032320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-08-19 00:01:00</td>\n",
       "      <td>10.089655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-08-19 00:02:00</td>\n",
       "      <td>10.923553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-08-19 00:03:00</td>\n",
       "      <td>11.660677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-08-19 00:04:00</td>\n",
       "      <td>10.049669</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 time        cpu\n",
       "0 2019-08-19 00:00:00   9.032320\n",
       "1 2019-08-19 00:01:00  10.089655\n",
       "2 2019-08-19 00:02:00  10.923553\n",
       "3 2019-08-19 00:03:00  11.660677\n",
       "4 2019-08-19 00:04:00  10.049669"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data={\n",
    "    'time':pd.date_range('2019-08-19',periods=200000,freq='T'),\n",
    "    'cpu':np.random.randn(200000)+10\n",
    "}\n",
    "df = pd.DataFrame(data,columns=['time','cpu'])  \n",
    "df.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 200000 entries, 0 to 199999\n",
      "Data columns (total 2 columns):\n",
      " #   Column  Non-Null Count   Dtype         \n",
      "---  ------  --------------   -----         \n",
      " 0   time    200000 non-null  datetime64[ns]\n",
      " 1   cpu     200000 non-null  float64       \n",
      "dtypes: datetime64[ns](1), float64(1)\n",
      "memory usage: 3.1 MB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3360</th>\n",
       "      <td>2019-08-21 08:00:00</td>\n",
       "      <td>9.133469</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3361</th>\n",
       "      <td>2019-08-21 08:01:00</td>\n",
       "      <td>9.602542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3362</th>\n",
       "      <td>2019-08-21 08:02:00</td>\n",
       "      <td>11.289115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3363</th>\n",
       "      <td>2019-08-21 08:03:00</td>\n",
       "      <td>11.548560</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3364</th>\n",
       "      <td>2019-08-21 08:04:00</td>\n",
       "      <td>8.760370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3365</th>\n",
       "      <td>2019-08-21 08:05:00</td>\n",
       "      <td>10.626256</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3366</th>\n",
       "      <td>2019-08-21 08:06:00</td>\n",
       "      <td>11.717693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3367</th>\n",
       "      <td>2019-08-21 08:07:00</td>\n",
       "      <td>9.577723</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3368</th>\n",
       "      <td>2019-08-21 08:08:00</td>\n",
       "      <td>9.033861</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3369</th>\n",
       "      <td>2019-08-21 08:09:00</td>\n",
       "      <td>8.828721</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3370</th>\n",
       "      <td>2019-08-21 08:10:00</td>\n",
       "      <td>10.737745</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    time        cpu\n",
       "3360 2019-08-21 08:00:00   9.133469\n",
       "3361 2019-08-21 08:01:00   9.602542\n",
       "3362 2019-08-21 08:02:00  11.289115\n",
       "3363 2019-08-21 08:03:00  11.548560\n",
       "3364 2019-08-21 08:04:00   8.760370\n",
       "3365 2019-08-21 08:05:00  10.626256\n",
       "3366 2019-08-21 08:06:00  11.717693\n",
       "3367 2019-08-21 08:07:00   9.577723\n",
       "3368 2019-08-21 08:08:00   9.033861\n",
       "3369 2019-08-21 08:09:00   8.828721\n",
       "3370 2019-08-21 08:10:00  10.737745"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[(df.time >='2019-08-21 08:00:00') &(df.time <='2019-8-21 08:10:00')]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 为了查看一个小时内CPU占用情况，现在是一分钟采样，数据太多，改造DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:00:00</th>\n",
       "      <td>2019-08-19 00:00:00</td>\n",
       "      <td>9.032320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:01:00</th>\n",
       "      <td>2019-08-19 00:01:00</td>\n",
       "      <td>10.089655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:02:00</th>\n",
       "      <td>2019-08-19 00:02:00</td>\n",
       "      <td>10.923553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:03:00</th>\n",
       "      <td>2019-08-19 00:03:00</td>\n",
       "      <td>11.660677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:04:00</th>\n",
       "      <td>2019-08-19 00:04:00</td>\n",
       "      <td>10.049669</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                   time        cpu\n",
       "time                                              \n",
       "2019-08-19 00:00:00 2019-08-19 00:00:00   9.032320\n",
       "2019-08-19 00:01:00 2019-08-19 00:01:00  10.089655\n",
       "2019-08-19 00:02:00 2019-08-19 00:02:00  10.923553\n",
       "2019-08-19 00:03:00 2019-08-19 00:03:00  11.660677\n",
       "2019-08-19 00:04:00 2019-08-19 00:04:00  10.049669"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##让时间作为索引，改造成5分钟一次作为索引，求5分钟的平均值\n",
    "s=pd.to_datetime(df.time)\n",
    "df.index=s\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:00:00</th>\n",
       "      <td>9.032320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:01:00</th>\n",
       "      <td>10.089655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:02:00</th>\n",
       "      <td>10.923553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:03:00</th>\n",
       "      <td>11.660677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:04:00</th>\n",
       "      <td>10.049669</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           cpu\n",
       "time                          \n",
       "2019-08-19 00:00:00   9.032320\n",
       "2019-08-19 00:01:00  10.089655\n",
       "2019-08-19 00:02:00  10.923553\n",
       "2019-08-19 00:03:00  11.660677\n",
       "2019-08-19 00:04:00  10.049669"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=df.drop('time',axis=1)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "DatetimeIndex: 200000 entries, 2019-08-19 00:00:00 to 2020-01-04 21:19:00\n",
      "Data columns (total 1 columns):\n",
      " #   Column  Non-Null Count   Dtype  \n",
      "---  ------  --------------   -----  \n",
      " 0   cpu     200000 non-null  float64\n",
      "dtypes: float64(1)\n",
      "memory usage: 3.1 MB\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-08-21 08:00:00</th>\n",
       "      <td>9.133469</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-21 08:01:00</th>\n",
       "      <td>9.602542</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-21 08:02:00</th>\n",
       "      <td>11.289115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-21 08:03:00</th>\n",
       "      <td>11.548560</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-21 08:04:00</th>\n",
       "      <td>8.760370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-21 08:05:00</th>\n",
       "      <td>10.626256</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-21 08:06:00</th>\n",
       "      <td>11.717693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-21 08:07:00</th>\n",
       "      <td>9.577723</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-21 08:08:00</th>\n",
       "      <td>9.033861</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-21 08:09:00</th>\n",
       "      <td>8.828721</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-21 08:10:00</th>\n",
       "      <td>10.737745</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           cpu\n",
       "time                          \n",
       "2019-08-21 08:00:00   9.133469\n",
       "2019-08-21 08:01:00   9.602542\n",
       "2019-08-21 08:02:00  11.289115\n",
       "2019-08-21 08:03:00  11.548560\n",
       "2019-08-21 08:04:00   8.760370\n",
       "2019-08-21 08:05:00  10.626256\n",
       "2019-08-21 08:06:00  11.717693\n",
       "2019-08-21 08:07:00   9.577723\n",
       "2019-08-21 08:08:00   9.033861\n",
       "2019-08-21 08:09:00   8.828721\n",
       "2019-08-21 08:10:00  10.737745"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['2019-08-21 08:00:00':'2019-08-21 08:10:00']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:00:00</th>\n",
       "      <td>9.032320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:01:00</th>\n",
       "      <td>10.089655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:02:00</th>\n",
       "      <td>10.923553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:03:00</th>\n",
       "      <td>11.660677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:04:00</th>\n",
       "      <td>10.049669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 23:55:00</th>\n",
       "      <td>11.115227</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 23:56:00</th>\n",
       "      <td>9.591975</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 23:57:00</th>\n",
       "      <td>10.348161</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 23:58:00</th>\n",
       "      <td>10.084976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 23:59:00</th>\n",
       "      <td>9.596509</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1440 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                           cpu\n",
       "time                          \n",
       "2019-08-19 00:00:00   9.032320\n",
       "2019-08-19 00:01:00  10.089655\n",
       "2019-08-19 00:02:00  10.923553\n",
       "2019-08-19 00:03:00  11.660677\n",
       "2019-08-19 00:04:00  10.049669\n",
       "...                        ...\n",
       "2019-08-19 23:55:00  11.115227\n",
       "2019-08-19 23:56:00   9.591975\n",
       "2019-08-19 23:57:00  10.348161\n",
       "2019-08-19 23:58:00  10.084976\n",
       "2019-08-19 23:59:00   9.596509\n",
       "\n",
       "[1440 rows x 1 columns]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['2019-8-19']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-08-19</th>\n",
       "      <td>9.999732</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-20</th>\n",
       "      <td>10.008316</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-21</th>\n",
       "      <td>10.003584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-22</th>\n",
       "      <td>9.979354</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-23</th>\n",
       "      <td>10.037609</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>10.001427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>10.015359</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>10.021580</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>10.040410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04</th>\n",
       "      <td>10.020062</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>139 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                  cpu\n",
       "2019-08-19   9.999732\n",
       "2019-08-20  10.008316\n",
       "2019-08-21  10.003584\n",
       "2019-08-22   9.979354\n",
       "2019-08-23  10.037609\n",
       "...               ...\n",
       "2019-12-31  10.001427\n",
       "2020-01-01  10.015359\n",
       "2020-01-02  10.021580\n",
       "2020-01-03  10.040410\n",
       "2020-01-04  10.020062\n",
       "\n",
       "[139 rows x 1 columns]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(df.index.date).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10.006269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9.984438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.028631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10.003651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9.994387</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>10.033311</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>10.003518</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>9.996456</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>10.006138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10.003119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>10.011707</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>10.023608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>10.009906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>9.997404</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>10.011372</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>10.018788</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>10.001193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>10.004066</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>9.987333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>10.007060</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>10.011626</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>10.013027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>9.984865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>9.999954</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            cpu\n",
       "time           \n",
       "0     10.006269\n",
       "1      9.984438\n",
       "2     10.028631\n",
       "3     10.003651\n",
       "4      9.994387\n",
       "5     10.033311\n",
       "6     10.003518\n",
       "7      9.996456\n",
       "8     10.006138\n",
       "9     10.003119\n",
       "10    10.011707\n",
       "11    10.023608\n",
       "12    10.009906\n",
       "13     9.997404\n",
       "14    10.011372\n",
       "15    10.018788\n",
       "16    10.001193\n",
       "17    10.004066\n",
       "18     9.987333\n",
       "19    10.007060\n",
       "20    10.011626\n",
       "21    10.013027\n",
       "22     9.984865\n",
       "23     9.999954"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(df.index.hour).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10.014576</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>10.009084</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>10.003427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>10.001654</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>10.006466</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>10.008262</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>10.001530</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>10.008499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>9.994231</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>9.992407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>10.010865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>10.010971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>10.011368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>10.005573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>10.005430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>10.003030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>10.028497</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>9.994103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>10.007341</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>10.002382</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            cpu\n",
       "time           \n",
       "1     10.014576\n",
       "34    10.009084\n",
       "35    10.003427\n",
       "36    10.001654\n",
       "37    10.006466\n",
       "38    10.008262\n",
       "39    10.001530\n",
       "40    10.008499\n",
       "41     9.994231\n",
       "42     9.992407\n",
       "43    10.010865\n",
       "44    10.010971\n",
       "45    10.011368\n",
       "46    10.005573\n",
       "47    10.005430\n",
       "48    10.003030\n",
       "49    10.028497\n",
       "50     9.994103\n",
       "51    10.007341\n",
       "52    10.002382"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(df.index.week).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>cpu</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:00:00</th>\n",
       "      <td>10.351175</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:05:00</th>\n",
       "      <td>9.897637</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:10:00</th>\n",
       "      <td>10.232680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:15:00</th>\n",
       "      <td>10.089818</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-08-19 00:20:00</th>\n",
       "      <td>9.847531</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04 20:55:00</th>\n",
       "      <td>9.932966</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04 21:00:00</th>\n",
       "      <td>10.275381</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04 21:05:00</th>\n",
       "      <td>9.373935</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04 21:10:00</th>\n",
       "      <td>9.330563</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-04 21:15:00</th>\n",
       "      <td>10.006192</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>40000 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                           cpu\n",
       "time                          \n",
       "2019-08-19 00:00:00  10.351175\n",
       "2019-08-19 00:05:00   9.897637\n",
       "2019-08-19 00:10:00  10.232680\n",
       "2019-08-19 00:15:00  10.089818\n",
       "2019-08-19 00:20:00   9.847531\n",
       "...                        ...\n",
       "2020-01-04 20:55:00   9.932966\n",
       "2020-01-04 21:00:00  10.275381\n",
       "2020-01-04 21:05:00   9.373935\n",
       "2020-01-04 21:10:00   9.330563\n",
       "2020-01-04 21:15:00  10.006192\n",
       "\n",
       "[40000 rows x 1 columns]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.resample('5T').mean() #5分钟采样一次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
